require(['./config'], ()=>{
    require(['template','header','footer','jquery'], (template,header)=>{
        class Buy {
            constructor(){
            this.render().then(()=>{
                this.calcTolalMoney()
                this.numChange()
                this.delshop()
            })
           
            }
            render(){
                return new Promise(resolve =>{
                    // 根据localstorage数据渲染
                const cart = localStorage.getItem('cart')
                if(cart){
                    this.cart = JSON.parse(cart)
                    $('#hejiBuy').html(template('cartTemplate',{cart:this.cart}))
                resolve()
                }
                
                })
                
            }
            calcTolalMoney(){
                // 从0算总价
                console.log(this.cart)
                let totalMoney = this.cart.reduce((total,shop)=>{
                    return total + shop.price*shop.count
                },0)
                $('#Paymoney').html(totalMoney)
            }
            numChange(){
                const _this = this
                $('.add').on('click',function(){
                    const id =$(this).parents('.qdan_list').data('id')
                    _this.cart = _this.cart.map(shop=>{
                        if(shop.id === id) {                            
                            $(this).prev().html(shop.count++)
                            $(this).parents('.qdan_list').find('.qd_340').html(shop.price*shop.count)
                        }
                        return shop
                    })
                    localStorage.setItem('cart', JSON.stringify(_this.cart))
                    _this.calcTolalMoney()
                })
                $('.reduce').on('click',function(){
                    const id =$(this).parents('.qdan_list').data('id')
                    _this.cart = _this.cart.map(shop=>{
                        if(shop.id === id) {
                                                   
                            $(this).next().html(shop.count--)
                            if(shop.count<1)   shop.count =1  
                            $(this).parents('.qdan_list').find('.qd_340').html(shop.price*shop.count)
                        }
                        return shop
                    })
                    localStorage.setItem('cart', JSON.stringify(_this.cart))
                    _this.calcTolalMoney()
                })

            }
            delshop(){
                const _this = this
                console.log($('#delshop'))
                $('.delshop').on('click',function(){
                    console.log(1)
                    if(confirm('确定要删除吗？')){
                        const id =$(this).parents('.qdan_list').data('id')
                        _this.cart = _this.cart.filter((item)=>{
                            if(item.id !==id){
                                return item
                            }
                        })
                        $(this).parents('.qdan_list').remove()
                        localStorage.setItem('cart',JSON.stringify(_this.cart))
                        //从新计算价格
                        _this.calcTolalMoney()
                    }
                })
            }

        } 
        new Buy()
    })
})